package baoxin.com.robotcoach;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import baoxin.com.robotcoach.adapter.MyPageAdapter;
import baoxin.com.robotcoach.adapter.PicAdapter;
import baoxin.com.robotcoach.adapter.TextAdapter;
import baoxin.com.robotcoach.bean.StepTx;
import baoxin.com.robotcoach.interfaces.GearObserver;
import baoxin.com.robotcoach.java.GearVerifier;
import baoxin.com.robotcoach.utils.MyLog;
import baoxin.com.robotcoach.utils.SpaceItemDecoration;
import baoxin.com.robotcoach.utils.TtsSpeaker;

public class PractiseActivity extends BLEActivity implements GearObserver {

    private ViewPager vp;


    Button start_stop, back;
    RecyclerView recyclerView_pic;
    RecyclerView recyclerView_text;
    PicAdapter picAdapter;
    TextAdapter textAdapter;
    MyPageAdapter myPageAdapter;

    private GearVerifier gearVerifier;
    private List<StepTx> stepTxList;//步骤文本

    LinearLayoutManager layoutManager2;

    int[] Gear5 = {//后6个为辅助数据
            R.drawable.gear_down, R.drawable.gear_1, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_2, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_3, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_4, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_5, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_4, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_3, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_2, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_1, R.drawable.gear_up,
            R.drawable.gear_down, R.drawable.gear_0, R.drawable.gear_up,
            R.drawable.gear_null, R.drawable.gear_null, R.drawable.gear_null, R.drawable.gear_null, R.drawable.gear_null, R.drawable.gear_null};


    int[] GearR = {R.drawable.gear_0, R.drawable.gear_1, R.drawable.gear_r, R.drawable.gear_0};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_practise);
        super.onCreate(savedInstanceState);
        Log.d("PractiseActivity", "onCreate");
        start_stop = findViewById(R.id.start_stop);
        start_stop.setOnClickListener(onClick);
        back = findViewById(R.id.back);
        back.setOnClickListener(onClick);

        recyclerView_pic = findViewById(R.id.recycler_view_pic);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView_pic.setLayoutManager(layoutManager);
        recyclerView_pic.addItemDecoration(new SpaceItemDecoration(5));
        picAdapter = new PicAdapter(Gear5);
        recyclerView_pic.setAdapter(picAdapter);

        gearVerifier = new GearVerifier(this, this);
        stepTxList = gearVerifier.getStepTxList();

        recyclerView_text = findViewById(R.id.recycler_view_text);
        layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView_text.setLayoutManager(layoutManager2);

        textAdapter = new TextAdapter(gearVerifier.getStepTxList());
        recyclerView_text.setAdapter(textAdapter);

        initViewPager();
    }


    View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.start_stop:
                    if (!practising) {
                        startPractise();
                    } else {
                        stopPractise();
                    }
                    break;
                case R.id.back:
                    finish();
                    break;
            }
        }
    };


    private void initViewPager() {
        vp = findViewById(R.id.view_pager);
        myPageAdapter = new MyPageAdapter();
        List<View> viewList = new ArrayList<>();
        for (int i = 0; i < Gear5.length; i++) {
            ImageView view = new ImageView(this);
            view.setImageResource(Gear5[i]);
            viewList.add(view);
        }
        myPageAdapter.setViewList(viewList);
        vp.setPageMargin(40);
        vp.setAdapter(myPageAdapter);

    }

    private boolean practising;

    @Override
    protected void signalChanged(String signal) {
        if (!practising) {
            return;
        }
        gearVerifier.filterSignal(signal);

    }


    @Override
    public void operationWrong() {
        TtsSpeaker.play("操作错误，请挂空挡后重新开始练习。");

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopPractise();
            }
        });
    }


    private void startPractise() {
        practising = true;
        start_stop.setText("结束练习");
        operationCorrect(0, false);

    }

    private void stopPractise() {
        practising = false;
        for (StepTx s : stepTxList) {
            s.setCurrent(false);
        }

        textAdapter.notifyDataSetChanged();
        scrollToPosition(0);
        start_stop.setText("开始练习");
    }

    @Override
    public void operationCorrect(final int index, boolean end) {

        if (end) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    stopPractise();
                }
            });

            return;
        }
        StepTx stepTx = stepTxList.get(index);
        TtsSpeaker.play(stepTx.getText());
        for (StepTx s : stepTxList) {
            s.setCurrent(false);
        }
        stepTx.setCurrent(true);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textAdapter.notifyDataSetChanged();
                scrollToPosition(index);

            }
        });

    }

    /**
     * 练习步骤-右侧文字列表
     *
     * @param index
     */
    private void scrollToPosition(int index) {
        int firstItem = layoutManager2.findFirstVisibleItemPosition();
        MyLog.i("junjie", "first = " + firstItem + ",index = " + index);
        if (index < firstItem) {

            recyclerView_text.smoothScrollToPosition(index);
            recyclerView_pic.smoothScrollToPosition(index);

        } else {

            recyclerView_text.smoothScrollToPosition(index + 9);
            recyclerView_pic.smoothScrollToPosition(index + 6);
        }
        vp.setCurrentItem(index);
    }

}
